﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

namespace ServerApp
{
   public class SendFileTCP
    {
        public string ReceiveName(string host)
        {
            ASCIIEncoding utf8 = new ASCIIEncoding();
            string filename = "";
            int port = 8181;
           // string host = "192.168.0.103";
            int size = 1400;
            IPAddress address = IPAddress.Parse(host);
            TcpListener listener = new TcpListener(address, port);
            listener.Start();
            Socket socket = listener.AcceptSocket();
            byte[] data = new byte[size];
            int flag = socket.Receive(data);
            string chuoi = utf8.GetString(data);
            string[] a = Regex.Split(chuoi, "\0");
            string ThongTin = a[0];
            filename = ThongTin;
            socket.Close();
            listener.Stop();

            return filename;
        }
        public int ReceiveContentfileTCP(string host,string filename)
        {
            int kq = 0;
            try
            {


                ASCIIEncoding utf8 = new ASCIIEncoding();
                int port = 8383;
                int size = 1400;
              //  string host = "192.168.0.103";
                IPAddress address = IPAddress.Parse(host);
                TcpListener listener = new TcpListener(address, port);
                // lắng nghe.
                listener.Start();//đường dẫn file

                // Khue has change to local location 
                // ->> old
               // String path = @"C:\Users\KimNhu\AppData\LocalLow\DATA\" + filename;
                // --> new 
                String path = @"D:\" + filename;


                //tạo file
                FileStream fs = new FileStream(path, FileMode.Create);
                //     MessageBox.Show("Server kết nối........");
                int flag = 0;
                int i = 0;
                while (flag != -1)
                {
                    Socket s = listener.AcceptSocket();
                    byte[] data = new byte[size];

                    for (int j = 0; j < data.Length; j++)
                    {
                        data[j] = 0;
                    }

                    try
                    {
                        flag = s.Receive(data);
                    }
                    catch(Exception e)
                    {
                        MessageBox.Show(e.ToString());
                    }
                    if (flag < 1400)
                    {
                        fs.Write(data, 0, flag);
                        flag = -1;
                    }
                    else
                    {
                        fs.Write(data, 0, data.Length);
                    }

                    s.Close();
                }
                kq = 1;
                fs.Close();
                listener.Stop();
                //MessageBox.Show("file được tạo");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return kq;
        }

     
    }
}
